package view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import socialnetwork.SocialNetwork;
import db.DAOFacade;
import db.Database;
import dto.Paper;
import dto.Researcher;

public class CellPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -494332364001873042L;

	/**
	 * Create the panel.
	 */
	
	private Researcher r;
	
	public CellPanel(Researcher r) {
		this.r = r;
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, 0.0,
				Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
		setLayout(gridBagLayout);
		JButton btnNewButton = new JButton("Combinar");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				combinar();
			}
		});
		btnNewButton.setToolTipText("Combinar");
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.fill = GridBagConstraints.BOTH;
		gbc_btnNewButton.insets = new Insets(0, 0, 0, 5);
		gbc_btnNewButton.gridx = 0;
		gbc_btnNewButton.gridy = 0;
		add(btnNewButton, gbc_btnNewButton);
	}
	
	private void combinar(){
		Vector<Researcher> investigadores = ResearcherListPanel.model.getResearchers();
		investigadores.remove(r);
		Collections.sort(investigadores, new Comparator<Researcher>() {
			@Override
			public int compare(Researcher o1, Researcher o2) {
				return o1.getName().compareTo(o2.getName());
			}
		});
		Researcher r2 = (Researcher) JOptionPane.showInputDialog(this.getParent(), "Seleccionar Investigador a fusionar:", "Fusionar Investigadores", JOptionPane.PLAIN_MESSAGE, null, investigadores.toArray(), null);
		if (r != null && r2 != null){
			String messaje = "Esta accion elimina el investigador " + r2.getName() + " (con ID de Conicet " + r2.getID() + ")\ny agregara sus Alias, Titulos, Articulos y Lugares de Trabajo al investigador " + r.getName() + " (con ID de Conicet " + r.getID() + ")";
			if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(this.getParent(), messaje, "Combinar Investigadores", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)){
				try {
					Database.getConnection();
					new DAOFacade().combinarInvestigadores(r2.getID_DB(), r.getID_DB());
					for (Paper p : r2.getPapers()){
						p.eliminarAuthor(r2);
						p.agregarAutor(r);
						r.agregarArticulo(p);
					}
					r.agregarAlias(r2.getAlias());
					r.agregarTitulos(r2.getTitles());
					SocialNetwork.eliminarInvestigador(r2);
					ResearcherListPanel.model.eliminarInvestigador(r2);
					new DAOFacade().guardarInvestigador(r);
					new DAOFacade().eliminarInvestigador(r2);
					Database.disconnect();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
}